package com.mar.tech.unify.service.database;

import com.mar.tech.unify.constants.QueueName;
import com.mar.tech.unify.factory.UnifyFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.serviceproxy.ServiceBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Description: 数据库访问 .<br>
 *
 * @author m-xy
 *     Created By 2019/12/3 18:41
 */
public class DatabaseServiceVerticle extends AbstractVerticle {
  private final static Logger logger = LoggerFactory.getLogger(DatabaseServiceVerticle.class);

  @Override
  public void start(Promise<Void> promise) throws Exception {
    DatabaseService.create(UnifyFactory.mysql(), ready -> {
      if (ready.succeeded()) {
        ServiceBinder binder = new ServiceBinder(vertx);
        binder.setAddress(QueueName.DATABASE_QUEUE).register(DatabaseService.class, ready.result());
        logger.info("[Vert.x] Register EventBus ".concat(QueueName.DATABASE_QUEUE));
        promise.complete();
      } else {
        promise.fail(ready.cause());
      }
    });
  }
}
